<?php

namespace app\modules\admin\controllers;

use yii\web\Controller;
use app\modules\admin\models\CwAdmin;
/**
 * Default controller for the `admin` module
 */
class DefaultController extends Controller
{
    /**
     * Renders the index view for the module
     * @return string
     */
    public function actionIndex()
    {
        if (\Yii::$app->request->isPost){
            $post = \Yii::$app->request->post();
            $model = CwAdmin::find(['admin_username' => $post['CwAdmin']['admin_username']])->one();
            if ($model != NULL){
                if ($model->admin_password == md5($post['CwAdmin']['admin_password'])){
                    \Yii::$app->session["admin"] = [
                        "isLogin" => "868",
                        "username" => $model->admin_username,
                        "password" => $model->admin_password,
                        "ID" => $model->admin_id,
                    ];
                    return $this->render('index');
                }else{
                    $this->layout = 'login';
                    $model = new CwAdmin;
                    $model->addError('admin_password','密码有误');
                    return $this->render('login',['model' => $model]);
                }
            }else{
                $this->layout = 'login';
                $model = new CwAdmin;
                $model->addError('admin_username','帐号不存在');
                return $this->render('login',['model' => $model]);
            }
        }
        
        if (\Yii::$app->session["admin"]["isLogin"] == "868"){
            return $this->render('index');
        }else{
            $this->layout = 'login';
            $model = new CwAdmin;
            return $this->render('login',['model' => $model]);
        }
    }
    
    public function actionLogout(){
        \Yii::$app->session->removeAll();
        return $this->redirect(['default/index']);
    }
}
